package com.xiaochen.baidu.service.thread;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaochen.baidu.service.IMoiuParseService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;

import java.util.HashMap;
import java.util.function.Supplier;

/**
 * @author xc
 * @date 2025/10/12/16:26
 * @desc
 */
@AllArgsConstructor
public class ClassThreadTask implements Supplier<String> {
    private String surl;
    private String pwd;
    private String password;
    private String dir;
    private String num;
    private IMoiuParseService moiuParseService;
    @SneakyThrows
    @Override
    public String get() {
        //dir:uk:shareId:randsk:fsId:server_filename:size
        String[] result = new String[7];
        int i = 0;
        if ("/".equals(dir)) {
            JsonNode node = exec();
            dir = node.get("list").get(0).get("path").asText();
        }
        JsonNode node = exec();
        result[i++] = dir;
        result[i++] = node.get("uk").asText();
        result[i++] = node.get("shareid").asText();
        result[i++] = node.get("randsk").asText();
        for (JsonNode j:node.get("list")){
            if (j.get("server_filename").asText().contains(num)){
                result[i++] = j.get("fs_id").asText();
                result[i++] = j.get("server_filename").asText();
                result[i++] = j.get("size").asText();
                break;
            }
        }
        //jiang result 转换为字符串
        return String.join(":", result);
    }

    private JsonNode exec() throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>() {{
            put("surl", surl);
            put("pwd", pwd);
            put("password", password);
            put("dir", dir);
        }};
        Call<ResponseBody> call = moiuParseService.list(map);
        ResponseBody responseBody = call.execute().body();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(responseBody.string());
        return jsonNode.get("data");
    }

}
